//
//  PMViewController.m
//  DBAChain
//
//  Created by Evan on 2018/9/5.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "PMViewController.h"
#import "CommonData.h"
#import "PMTabBarController.h"
#import "UserCenter.h"
#import "LoginNavigationController.h"
#import "DKProgressHUD.h"

@interface PMViewController ()



@end

@implementation PMViewController

- (id)init
{
    self = [super init];
    if (self) {
        self.isPush = NO;
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.isNeedLogin = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    if([self.navigationController.viewControllers count] >= 2){
        [self setDefaultBackBtn];
    }
    [self setHeadView];
    //更新用户信息
    if ([UserCenter shareCenter].user.phone.length <= 0) {
        [[UserCenter shareCenter] getProfiles:^(dataGetUserInfo *profile) {
        }];
    }
}

-(void)dealloc{
    [DKProgressHUD dismiss];
}

-(void)viewWillAppear:(BOOL)animated{
    
    if (self.navigationController.navigationBar.hidden == YES) {
        self.navigationController.navigationBar.hidden = NO;
    }
    self.hidesBottomBarWhenPushed = YES;
    if (self.tabBarController.tabBar.hidden == NO) {
        self.tabBarController.tabBar.hidden = YES;
    }
    
    if (self.isNeedLogin) {
        BOOL isLogin = [[UserCenter shareCenter] isLogined];
        if (!isLogin) {
            LoginNavigationController *login = [[LoginNavigationController alloc]init];
            [self presentViewController:login animated:YES completion:nil];
            return;
        }
    }
    //默认设置状态栏位白色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


///设置通知栏为黑色
-(void)setBlackStatus:(BOOL)isBlack{
    if (isBlack) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    }else{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }
}

#pragma mark - 头部视图
-(void)setHeadView{
    self.supheadView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,  KScreenWidth, distanceHeight)];
    self.supheadView.image = [UIImage imageNamed:@"icon_head"];
    [self.view addSubview:self.supheadView];
    [self.view sendSubviewToBack:self.supheadView];
}

-(void)hideHeadView{
    self.supheadView.hidden = YES;
}

-(void)changeHeadView{
    self.supheadView.frame = CGRectMake(0, 0,  KScreenWidth, 200);
}

#pragma mark - 导航按钮事件
//设置
- (UIButton *)backBtnImg{
    if(!_backBtnImg){
        UIImage *img = [UIImage imageNamed:@"nav_back_btn_2"];
        _backBtnImg = [[UIButton alloc]initWithFrame:CGRectMake(20, 35, 16, 25)];
        _backBtnImg.hidden = YES;
        [_backBtnImg setBackgroundImage:img forState:UIControlStateNormal];
        [_backBtnImg addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_backBtnImg];
    }
    return _backBtnImg;
}

//设置navition的返回按钮
- (void)setDefaultBackBtn
{
    UIButton* backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(0.0, 0.0, 30.0, 40.0)];
    [backBtn setImage:[UIImage imageNamed:@"nav_back_btn_2"]
             forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}

- (void)setLeftBarButton:(UIButton*)button
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)setRightBarButton:(UIButton*)button
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)goBack:(id)sender
{
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

//返回的tabBar的首页
- (void)goRootBack:(NSInteger)tabIndex{
    // 如果不是 Modal 窗体, 说明login 页面是主页面, 那么实例化真实首页, 进入应用
    PMTabBarController *tabView=(PMTabBarController*)[UIApplication sharedApplication].keyWindow.rootViewController;
    tabView.selectedIndex = tabIndex;
    UINavigationController *pmView = (UINavigationController*)tabView.viewControllers[tabView.selectedIndex];
    [pmView popToRootViewControllerAnimated:YES];
    
}

@end
